from tools.transmit_receive.send_unit import SendUnit
from .exception import TemplateException
from typing import List


class TimespanGiver:
    """
    时间间隔赋予器
    """

    @staticmethod
    def create_units(val_sequence: List[str], time_sequence: List[float]) -> List[SendUnit]:
        """
        为“数据包序列” 赋予 “时间间隔序列”
        :param val_sequence: <list-str> 数据包序列
        :param time_sequence: <list-integer> 时间间隔序列
        :return: <list-SendUnit> 发送单元序列
        """
        if len(val_sequence) != len(time_sequence) + 1:
            raise TemplateException("val_sequence length is not matched with time_sequence")
        ans = []
        timelen = len(time_sequence)
        for i in range(timelen):
            ans.append(SendUnit(val_sequence[i], time_sequence[i]))
        ans.append(SendUnit(val_sequence[timelen], 0))
        return ans
